package com.fulu.game.bi.service.impl.product;

import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.common.util.math.MathUtils;
import com.fulu.game.bi.entity.dto.FunnelAnalyseDetailListDTO;
import com.fulu.game.bi.entity.vo.product.FunnelAnalyseListVO;
import com.fulu.game.bi.hbase.BehaviourFunnelStatDao;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;

@Service
@Slf4j
public class BehaviourFunnelStatService {
    @Autowired
    private BehaviourFunnelStatDao behaviourFunnelStatDao;

    public PageInfo<FunnelAnalyseListVO> list(FunnelAnalyseDetailListDTO dto) {
        dto.setPageNum((dto.getPageNum() - 1) * dto.getPageSize());
        List<FunnelAnalyseListVO> list = behaviourFunnelStatDao.list(dto);
        Integer count = behaviourFunnelStatDao.count(dto);
        list.forEach(map -> {
            map.setTotalTransNum(getTransNum(map));
            map.setTotalTransRatio(MathUtils.ratio(getTransNum(map), map.getFirstEventUserNum()));
        });
        PageInfo<FunnelAnalyseListVO> pageInfo = new PageInfo<>();
        pageInfo.setList(list);
        pageInfo.setTotal(count);
        return pageInfo;
    }

    private Integer getTransNum(FunnelAnalyseListVO map) {
        Integer totalTransNum = 0;
        switch (map.getStepNum()) {
            case 1:
                totalTransNum = map.getUserNum1();
                map.setUserNum2(null);
                map.setUserNum2Ratio(null);
                map.setUserNum3(null);
                map.setUserNum3Ratio(null);
                map.setUserNum4(null);
                map.setUserNum4Ratio(null);
                map.setUserNum5(null);
                map.setUserNum5Ratio(null);
                break;
            case 2:
                totalTransNum = map.getUserNum2();
                map.setUserNum3(null);
                map.setUserNum3Ratio(null);
                map.setUserNum4(null);
                map.setUserNum4Ratio(null);
                map.setUserNum5(null);
                map.setUserNum5Ratio(null);
                break;
            case 3:
                totalTransNum = map.getUserNum3();
                map.setUserNum4(null);
                map.setUserNum4Ratio(null);
                map.setUserNum5(null);
                map.setUserNum5Ratio(null);
                break;
            case 4:
                totalTransNum = map.getUserNum4();
                map.setUserNum5(null);
                map.setUserNum5Ratio(null);
                break;
            case 5:
                totalTransNum = map.getUserNum5();
                break;
            default:
                break;
        }
        return totalTransNum;
    }

    public void listExport(FunnelAnalyseDetailListDTO dto) {
        String fileName = "漏斗分析列表";
        LinkedHashMap<String, Mapper<FunnelAnalyseListVO>> map = new LinkedHashMap<>();
        map.put("日期", v -> v.getStatDate());
        map.put("漏斗名称", v -> v.getFunnelName());
        map.put("总体转化人数", v -> v.getTotalTransNum() + "");
        map.put("总体转化比率", v -> v.getTotalTransRatio() == null ? "/" : v.getTotalTransRatio().toString());
        map.put("开始事件", v -> v.getFirstEventUserNum() + "");
        map.put("第1步转化人数", v -> v.getUserNum1() == null ? "/" : v.getUserNum1() + "");
        map.put("第1步转化比率", v -> v.getUserNum1Ratio() == null ? "/" : v.getUserNum1Ratio().toString());
        map.put("第2步转化人数", v -> v.getUserNum2() == null ? "/" : v.getUserNum2() + "");
        map.put("第2步转化比率", v -> v.getUserNum2Ratio() == null ? "/" : v.getUserNum2Ratio().toString());
        map.put("第3步转化人数", v -> v.getUserNum3() == null ? "/" : v.getUserNum3() + "");
        map.put("第3步转化比率", v -> v.getUserNum3Ratio() == null ? "/" : v.getUserNum3Ratio().toString());
        map.put("第4步转化人数", v -> v.getUserNum4() == null ? "/" : v.getUserNum4() + "");
        map.put("第4步转化比率", v -> v.getUserNum4Ratio() == null ? "/" : v.getUserNum4Ratio().toString());
        map.put("第5步转化人数", v -> v.getUserNum5() == null ? "/" : v.getUserNum5() + "");
        map.put("第5步转化比率", v -> v.getUserNum5Ratio() == null ? "/" : v.getUserNum5Ratio().toString());
        Querier querier = (num) -> list(dto);
        CsvExportUtil.export(100, fileName, map, querier);
    }
}
